<template>
  <div class="app-container">
    <!--工具栏-->
    <div class="head-container">
      <!--表格渲染-->
      <div v-if="crud.props.searchToggle" style="display: flex;">
        <label class="el-form-item-label">物料编号</label>
        <el-input v-model.trim="query.productNo" clearable placeholder="物料编号" style="width: 180px;" class="filter-item" @keyup.enter.native="crud.toQuery" @clear="crud.toQuery" />
        <label class="el-form-item-label">标签号</label>
        <el-input v-model.trim="query.serialNo" clearable placeholder="标签号" style="width: 180px;" class="filter-item" @keyup.enter.native="crud.toQuery" @clear="crud.toQuery" />
        <label class="el-form-item-label">rpa状态</label>
        <el-select v-model="query.rpaStatus" placeholder="请选择字段名" style="width: 180px;" class="filter-item" @change="crud.toQuery">
          <el-option
            v-for="item in dict.rpa_status"
            :key="item.value"
            :label="item.label"
            :value="item.value"
          />
        </el-select>
        <label class="el-form-item-label">生产时间</label>
        <el-date-picker
          v-model="query.creationDate"
          class="filter-item"
          style="width: 185px;"
          type="date"
          placeholder="请选择"
          value-format="yyyy-MM-dd"
          @change="crud.toQuery"
        />
        <label class="el-form-item-label">结束日期</label>
        <el-date-picker
          v-model="query.finishDate"
          class="filter-item"
          style="width: 185px;"
          type="date"
          placeholder="请选择"
          value-format="yyyy-MM-dd"
          @change="crud.toQuery"
        />
        <div style="margin-left: 10px;">
          <el-button type="success" size="mini" class="filter-item" icon="el-icon-search" @click="crud.toQuery">搜索</el-button>
          <el-button type="warning" size="mini" class="filter-item" icon="el-icon-refresh" @click="crud.resetQuery()">重置</el-button>
        </div>
      </div>
      <crudOperation :permission="permission">
        <el-button slot="left" type="primary" class="filter-item" size="mini" icon="el-icon-plus" @click="visible = true">新增物料</el-button>
        <el-button slot="left" type="danger" class="filter-item" size="mini" icon="el-icon-folder-delete" :loading="btn.cancel" :disabled="crud.selections.length === 0" @click="cancelValid">取消审核</el-button>
        <el-button slot="left" type="success" class="filter-item" size="mini" icon="el-icon-document-checked" :disabled="crud.selections.length === 0" @click="batchAdd">批量审核</el-button>
      </crudOperation>
      <el-table ref="table" v-loading="crud.loading" :data="crud.data" style="width: 100%" @selection-change="crud.selectionChangeHandler">
        <el-table-column type="selection" width="55" />
        <el-table-column prop="productNo" label="物料编号" />
        <el-table-column prop="serialNo" label="标签号" />
        <el-table-column prop="createTime" label="入库时间" />
        <el-table-column prop="updateName" label="更新人" />
        <el-table-column prop="inputMode" label="录入方式">
          <template slot-scope="scope">
            {{ dict.label.input_mode[scope.row.inputMode] }}
          </template>
        </el-table-column>
        <el-table-column prop="rpaStatus" label="RPA状态">
          <template slot-scope="scope">
            <el-tag v-if="scope.row.rpaStatus === 0" type="primary">{{ dict.label.rpa_status[scope.row.rpaStatus] }}</el-tag>
            <el-tag v-else-if="scope.row.rpaStatus === 1" type="success">{{ dict.label.rpa_status[scope.row.rpaStatus] }}</el-tag>
            <el-tag v-else-if="scope.row.rpaStatus === 3" type="warning">{{ dict.label.rpa_status[scope.row.rpaStatus] }}</el-tag>
            <el-tag v-else type="danger">{{ dict.label.rpa_status[scope.row.rpaStatus] }}</el-tag>
          </template>
        </el-table-column>
        <el-table-column prop="qty" label="数量" />
        <el-table-column prop="productDescription" label="物料描述" />
        <el-table-column prop="partnerProductNo" label="客户物料编码" />
        <el-table-column prop="reviewer" label="审阅人" />
        <!-- <el-table-column prop="creationDate" label="生产日期" /> -->
        <!-- <el-table-column prop="createTime" label="创建时间" /> -->
        <el-table-column prop="receiverName" label="客户" />
        <el-table-column prop="netWeight" label="净重" />
        <el-table-column prop="weightUom" label="重量单位" />
        <el-table-column label="判定结果选择" width="150px" align="center">
          <template slot-scope="scope">
            <el-button type="success" size="mini" :disabled="scope.row.rpaStatus !== 0" @click="open(scope.row)">结果选择</el-button>
            <!-- <el-button v-permission="['admin','rap:audit']" type="danger" size="mini">审核</el-button> -->
          </template>
        </el-table-column>
      </el-table>
      <pagination />
      <!-- 表单渲染 -->
      <el-dialog
        title="新增物料"
        :visible.sync="visible"
        width="500px"
        :before-close="handleClose"
      >
        <el-form ref="addForm" :model="form" :rules="addRules">
          <el-form-item label="录入方式" :label-width="formLabelWidth">
            <el-select v-model="form.inputMode" placeholder="请选择" class="filter-item" style="width: 300px;">
              <el-option
                v-for="item in options"
                :key="item.value"
                :label="item.label"
                :value="item.value"
              />
            </el-select>
          </el-form-item>
          <div v-if="form.inputMode === 0">
            <el-form-item label="标签号" :label-width="formLabelWidth" prop="serialNo">
              <el-input v-model="form.serialNo" style="width: 300px;" />
            </el-form-item>
            <el-form-item label="料号" :label-width="formLabelWidth" prop="productNo">
              <el-input v-model="form.productNo" style="width: 300px;" />
            </el-form-item>
            <el-form-item label="数量" :label-width="formLabelWidth" prop="qty">
              <el-input v-model="form.qty" style="width: 300px;" />
            </el-form-item>
            <!-- <el-form-item label="物料描述" :label-width="formLabelWidth" prop="productDescription">
              <el-input v-model="form.productDescription" style="width: 300px;" />
            </el-form-item>
            <el-form-item label="客户" :label-width="formLabelWidth" prop="customer">
              <el-input v-model="form.customer" style="width: 300px;" />
            </el-form-item> -->
            <el-form-item label="客户物料编码" :label-width="formLabelWidth" prop="partnerProductNo">
              <el-input v-model="form.partnerProductNo" style="width: 300px;" />
            </el-form-item>
            <el-form-item label="开始时间" :label-width="formLabelWidth" prop="creationDate">
              <el-date-picker
                v-model="form.creationDate"
                class="filter-item"
                style="width: 300px;"
                type="datetime"
                placeholder="选择日期"
                value-format="yyyy-MM-dd HH:mm:ss"
              />
            </el-form-item>
            <el-form-item label="结束时间" :label-width="formLabelWidth" prop="finishDate">
              <el-date-picker
                v-model="form.finishDate"
                class="filter-item"
                style="width: 300px;"
                type="datetime"
                placeholder="选择日期"
                value-format="yyyy-MM-dd HH:mm:ss"
              />
            </el-form-item>
          </div>
          <div v-else>
            <el-form-item label="标签号" :label-width="formLabelWidth" prop="serialNo">
              <el-input v-model="form.serialNo" style="width: 300px;" />
            </el-form-item>
            <el-form-item label="料号" :label-width="formLabelWidth" prop="productNo">
              <el-input v-model="form.productNo" style="width: 300px;" />
            </el-form-item>
            <el-form-item label="数量" :label-width="formLabelWidth" prop="qty">
              <el-input v-model="form.qty" style="width: 300px;" />
            </el-form-item>
            <el-form-item label="物料描述" :label-width="formLabelWidth" prop="productDescription">
              <el-input v-model="form.productDescription" style="width: 300px;" />
            </el-form-item>
            <el-form-item label="客户" :label-width="formLabelWidth" prop="customer">
              <el-input v-model="form.customer" style="width: 300px;" />
            </el-form-item>
            <el-form-item label="客户物料编码" :label-width="formLabelWidth" prop="partnerProductNo">
              <el-input v-model="form.partnerProductNo" style="width: 300px;" />
            </el-form-item>
            <el-form-item label="开始时间" :label-width="formLabelWidth" prop="creationDate">
              <el-input v-model="form.creationDate" format="yyyy-MM-dd HH:mm:ss" style="width: 300px;" />
            </el-form-item>
            <el-form-item label="结束时间" :label-width="formLabelWidth" prop="finishDate">
              <el-input v-model="form.finishDate" format="yyyy-MM-dd HH:mm:ss" style="width: 300px;" />
            </el-form-item>
          </div>
        </el-form>
        <span slot="footer" class="dialog-footer">
          <el-button @click="visible = false">取 消</el-button>
          <el-button type="primary" @click="submitAdd">确认新增</el-button>
        </span>
      </el-dialog>
      <!-- 选择表单 -->
      <el-dialog
        title="判定结果选择"
        :visible.sync="selectVisible"
        width="500px"
        :before-close="selectClose"
        :close-on-click-modal="false"
      >
        <span style="width: 100%;display: flex;gap: 20px;">
          <el-button style="width: 50%;" type="success" :loading="btn.pass" :disabled="btn.fail === true || btn.emergency === true" @click="submitValidate(1)">实验通过</el-button>
          <el-button style="width: 50%;" type="danger" :loading="btn.emergency" :disabled="btn.fail === true || btn.pass === true" @click="submitValidate(3)">紧急发货</el-button>
          <el-button style="width: 50%;" type="primary" :loading="btn.fail" :disabled="btn.pass === true || btn.emergency === true" @click="submitValidate(2)">实验不通过</el-button>
        </span>
      </el-dialog>
      <!-- 全部审核 -->
      <el-dialog
        title="所有待审物料判定结果选择"
        :visible.sync="allVisible"
        width="500px"
        :close-on-click-modal="false"
      >
        <span style="width: 100%;display: flex;gap: 20px;">
          <el-button style="width: 50%;" type="success" :loading="btn.pass" :disabled="btn.fail === true || btn.emergency === true" @click="submitAllValidate(1)">实验通过</el-button>
          <el-button style="width: 50%;" type="danger" :loading="btn.emergency" :disabled="btn.fail === true || btn.pass === true" @click="submitAllValidate(3)">紧急发货</el-button>
          <el-button style="width: 50%;" type="primary" :loading="btn.fail" :disabled="btn.pass === true || btn.emergency === true" @click="submitAllValidate(2)">实验不通过</el-button>
        </span>
      </el-dialog>
    </div>
  </div>
</template>

<script>
import crudRpaInformation from '@/api/work/rpaInformation'
import CRUD, { presenter, header, form, crud } from '@crud/crud'
import crudOperation from '@crud/CRUD.operation'
import pagination from '@crud/Pagination'

const defaultForm = { id: null, productNo: null, materialDescription: null, serialNo: null, customer: null, customerMaterialCode: null, productionTime: null, createTime: null, finishTime: null, inputMode: null, rpaStatus: null, result: null, cartonNo: null, qty: null, createName: null }
export default {
  name: 'RpaIsolation',
  components: { crudOperation, pagination },
  mixins: [presenter(), header(), form(defaultForm), crud()],
  cruds() {
    return CRUD({ title: 'RPA', url: 'api/rpaInformation', idField: 'id', sort: 'id,desc', crudMethod: { ...crudRpaInformation },
      optShow: {
        add: false,
        edit: false,
        reset: false,
        download: true,
        del: false
      }
    })
  },
  dicts: ['rpa_status', 'input_mode'],
  data() {
    return {
      visible: false,
      selectVisible: false,
      form: {},
      selectLabel: null,
      options: [
        {
          label: '手动输入',
          value: 0
        },
        {
          label: '扫码录入',
          value: 1
        }
      ],
      allVisible: false,
      permission: {
        add: ['admin', 'rpaInformation:add', 'otherUser'],
        edit: ['admin', 'rpaInformation:edit', 'otherUser'],
        del: ['admin', 'rpaInformation:del', 'otherUser']
      },
      formLabelWidth: '120px',
      addRules: {
        inputMode: [
          { required: true, message: '请选择录入方式', trigger: 'change' }
        ],
        serialNo: [
          { required: true, message: '请输入标签号', trigger: 'blur' }
        ],
        productNo: [
          { required: true, message: '请输入料号', trigger: 'blur' }
        ],
        qty: [
          { required: true, message: '请输入数量', trigger: 'blur' }
        ],
        // productDescription: [
        //   { required: true, message: '请输入物料描述', trigger: 'blur' }
        // ],
        creationDate: [
          { required: true, message: '请选择开始时间', trigger: 'change' }
        ],
        finishDate: [
          { required: true, message: '请选择结束时间', trigger: 'change' }
        ]
      },
      btn: {
        pass: false,
        fail: false,
        cancel: false,
        emergency: false
      }
    }
  },
  methods: {
    // 钩子：在获取表格数据之前执行，false 则代表不获取数据
    [CRUD.HOOK.beforeRefresh]() {
      return true
    },
    isRowSelectable(val) {
      if (val.rpaStatus === 0) return true
      return false
    },
    batchAdd() {
      this.allVisible = true
    },
    async cancelValid() {
      try {
        this.btn.cancel = true
        const res = await crudRpaInformation.cancelValidAPI({
          serialNoList: this.crud.selections.map(item => item.serialNo)
        })
        if (res.result === 'OK') {
          this.$message.success('取消验证成功')
          this.crud.toQuery()
        } else {
          this.$message.error(res.data)
        }
      } finally {
        this.btn.cancel = false
      }
    },
    handleClose() {
      this.visible = false
    },
    handlerQuery() {
    },
    open(row) {
      this.selectLabel = row.serialNo
      this.selectVisible = true
    },
    selectClose() {
      this.selectVisible = false
      this.form = {}
    },
    async submitAdd() {
      try {
        const res = await crudRpaInformation.addAPI(this.form)
        if (res.result === 'OK') {
          this.$message.success('添加成功')
          this.visible = false
          this.crud.refresh()
        } else {
          this.$message.error('添加失败')
          this.visible = false
        }
      } finally {
        this.crud.refresh()
        this.visible = false
      }
    },
    async submitValidate(val) {
      try {
        if (val === 1) {
          this.btn.pass = true
        } else if (val === 3) {
          this.btn.emergency = true
        } else {
          this.btn.fail = true
        }
        const rpaStatus = val
        const res = await crudRpaInformation.submitValidateAPI({
          serialNo: this.selectLabel,
          rpaStatus
        })
        if (res.result === 'OK') {
          this.$message.success('操作成功')
        } else {
          this.$message.warning('操作失败')
        }
        this.selectVisible = false
        this.crud.refresh()
      } finally {
        this.btn.pass = false
        this.btn.fail = false
        this.btn.emergency = false
      }
    },
    async submitAllValidate(val) {
      try {
        if (val === 1) {
          this.btn.pass = true
        } else if (val === 3) {
          this.btn.emergency = true
        } else {
          this.btn.fail = true
        }
        const rpaStatus = val
        const res = await crudRpaInformation.submitAllValidateAPI({
          rpaStatus,
          serialNoList: this.crud.selections.map(item => item.serialNo)
        })
        if (res.result === 'OK') {
          this.$message.success('批量操作成功')
        } else {
          this.$message.warning(res.message || '批量验证提交失败，请重试')
        }
        this.allVisible = false
        this.crud.refresh()
      } finally {
        this.btn.pass = false
        this.btn.fail = false
        this.btn.emergency = false
      }
    }
  }
}
</script>

<style scoped lang="scss">
</style>
